package net.uhuijia.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import java.io.InputStream;
import java.nio.charset.Charset;

/*@Component*/
public class ErrorFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "error";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        System.out.println(ctx.getResponseBody());
        try{
            InputStream stream = ctx.getResponseDataStream();
            String body = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
            String returnStr = body;
            //你的处理逻辑，加密，添加新的返回值等等.....			             /
            // / 内容重新写入
            ctx.setResponseBody(returnStr);
        }catch (Exception e){
            e.printStackTrace();
        }


        //---------------------
        return null;
    }
}
